//
//  panelManager.h
//  Void Dead
//
//  Created by Sidney Just on 04.07.09.
//  Copyright 2009 by Sidney Just
//  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; 
//  either version 3 of the License, or (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
//  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//  

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGLDrawable.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@class VD_TextureManager;
@class VD_Panel;
@class VD_Engine;
@class VD_Vector;
@class VD_Animation;
@class VD_ParticleEmitter;

typedef enum {
	kDefaultMode,
	kAlphaReckoningMode
} kTouchCheck;

/**
 * This class can be used to create panels, animations and particle emitters.
 * A instance of this class will be provided by the VD_Engine class
 **/
@interface VD_PanelManager : NSObject {
	CGPoint touchWasIn;
	
	GLfloat panelCamera_x;
	GLfloat panelCamera_y;
	
	int aktPanels;
	
	@private
	int emptyPanels;
	int lastLayer;
	
	VD_Panel **panels;
	
	VD_ParticleEmitter **emitter;
	int emitterCount;
	
	VD_Animation **animations;
	int aktAnimations;
	
	VD_Engine *render_view;
	VD_TextureManager *texMan;
	BOOL lastTexHadAlpha;
	
	kTouchCheck touchSelectMode;
	int alphaThreshold;
	
	int polyCount;
	int drawCalls;
	int drawVertices;
}

/**
 * The position where the point from isTouchInPanel has hit the panel
 * available after the first isTouchInPanel call.
 **/
@property CGPoint touchWasIn;

/**
 * The mode for that will be used for isTouchInPanel: calls.
 * If this is set to kDefaultMode, the isTouchInPanel: function will not care for alpha values
 * If this is set to kAlphaReckoningMode, the is isTouchInPanel: will also check the alpha value of the point
 **/
@property kTouchCheck touchSelectMode;

/**
 * The threshold for the alpha, every alpha value under this threshold (255-threshold) will be treated as insivible
 * Default: 8
 **/
@property int alphaThreshold;

/**
 * These vars can be used to set all panels to a new position
 * The panels will be set by adding the camera vars to the old position
 * This is faster than reposition every panel manually!
 **/
@property GLfloat panelCamera_x, panelCamera_y;

/**
 * The panel count
 **/
@property int aktPanels;

/**
 * Creates a new particle emitter
 @return Pointer to the emitter
 **/
- (VD_ParticleEmitter *)createParticleEmitter;

/**
 * Creates a new Panel
 @return Pointer to the panel
 **/
- (VD_Panel *)createPanel:(int)layer;

/**
 * Draws every visible Panel on the screen and calculates the animation for animated panels
 **/
- (void)drawAll;

/**
 * Inits the panel manager with the given VD_Engine
 @param eng pointer to the VD_Engine
 **/
- (id)initWithEngine:(VD_Engine *)eng;

/**
 * Creates a new VD_Animation object and sets it with the given paramaters.
 * The given panel (pan) gets automatically animated with the new VD_Animation object.
 @param pan Panel which gets animated
 @param startPoint the starting Point in pixels and x coordinates. If it is -1 it will use the latest atlas pos
 @param endPoint end of the animation atlas in pixels and x coordinates.
 @param speed the speed of the animation in seconds
 @param tileSize the size of one animation in pixels and x coordinates
 @param leap the tiles that will be leaped per frame
 @return Returns the pointer to the VD_Animation object. Can be used to share the animation with other panels
 **/
- (VD_Animation *)createAnimation:(VD_Panel *)pan withStart:(float)startPoint End:(float)endPoint Speed:(float)speed Size:(float)tileSize andLeap:(float)leap;

/**
 * Updates the given panel (pan)
 * This function updates automatically the size of the panel, calculates the texture coordinates for the atlas mapping and sets the animation
 * Call this function when ever you update the texture, atlas mapping or the animation!
 @param pan Panel which should be updated
 @param at yes if the Atlas mapping has changed or no when the atlas didn't changed
 @param anim pointer to a new animation or NULL when the animation shouldn't be changed
 **/
- (void)updatePanel:(VD_Panel *)pan withAtlas:(BOOL)at forAnimation:(VD_Animation *)anim;

/**
 * Checks if a Panel is touched. Sets the touchWasIn varibale
 @param pos the point of the touch
 @return the pointer to the panel which is hit by the touch or NULL if no panel were hit
 **/
- (VD_Panel *)isTouchInPanel:(CGPoint)pos;

/**
 * Checks if a Panel is touched. Sets the touchWasIn varibale
 @param pos the point of the touch
 @param layer the layer which should be checked
 @return the pointer to the panel which is hit by the touch or NULL if no panel were hit
 **/
- (VD_Panel *)isTouchInPanel:(CGPoint)pos andInLayer:(int)layer;

/**
 * Deletes a particle emitter from the stack.
 @param emit Emitter that should be deleted
 **/
- (void)deleteParticleEmitter:(VD_ParticleEmitter *)emit;

/**
 * Deletes the given Panel (the panel must be handled by this panel manager!)
 @param pan Panel that should be deleted
 **/
- (void)deletePanel:(VD_Panel *)pan;

/**
 * Deletes the given animation (the animation must be handled by this panel manager!)
 @param ani animation that should be deleted
 **/
- (void)deleteAnimation:(VD_Animation *)ani;

/**
 * This function traces between two points and checks if a panel is hit
 @param start The start point for the trace
 @param end The end point for the trace
 **/
- (VD_Panel *)p_trace:(CGPoint)start :(CGPoint)end;


/**
 * This function returns the pointer to the next panel on the stack. You can use this in a loop to switch through all panels on the stack
 @param pan A panel or Null when you want the first panel on the stack 
 @return the pointer to the next panel on the stack or null when no next panel exist on the stack
 **/
- (VD_Panel *)nextPanel:(VD_Panel *)pan;

/**
 * This function creates a default atlas mapping for the panel.
 * The atlas mapping will be set to fit the whole texture!
 **/
- (void)createDefaultShape:(VD_Panel *)pan;

@end
